home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Communication / I-Spy scripts / Filter and concatenate HTML.pl < prev    next >
Perl Script  |  1995-12-24  |  3KB  |  110 lines

  1. #!/usr/bin/perl
  2.  
  3. #    Filter and concatenate HTML.pl
  4. #        This script appends lines from one file to another, ignoring those lines that match a filter string
  5. #        The output is an HTML document
  6. #
  7. #        12/24/95 Version 1.1.1
  8. #        This script is free, public domain, no strings attached.
  9. #     Igor Livshits <igorl@uiuc.edu>
  10.  
  11.  
  12. $openToAppend=                 ">> ";
  13. $macPathDelimiter=         ":";
  14. $unixPathDelimiter=     "/";
  15. $argumentCounter=            0;
  16. $newLineDelimiter=        "\n";
  17.  
  18. $omitFilterTag=                "#-";
  19. $keepFilterTag=                "#+";
  20. $true=                                (1 == 1);
  21. $false=                                (1 == 0);
  22. $htmlListItemLeader=    "<LI> <A HREF= \"";
  23. $htmlListItemMiddle=    "\">";
  24. $htmlListItemTrailer=    "</A>" . $newLineDelimiter;
  25.  
  26.  
  27. #    Read in the arguments passed to us from the AppleScript agent
  28. #
  29. $workingDirectory=  $ARGV[$argumentCounter++] . $macPathDelimiter;
  30. $dataDirectory=            $ARGV[$argumentCounter++] . $macPathDelimiter;
  31. $linesAddedFile=         $ARGV[$argumentCounter++];
  32. $filtersFile=                $ARGV[$argumentCounter++];
  33. $newFilesFile=            $ARGV[$argumentCounter++];
  34. $urlFile=                        $ARGV[$argumentCounter++];
  35.  
  36.  
  37. #    Read the URL
  38. #
  39. open(urlFile, $dataDirectory . $urlFile) || die "Cannot open $urlFile: $!";
  40. $rootURL= <urlFile>;
  41. if ($rootURL =~ /$newLineDelimiter$/) { chop ($rootURL); }
  42. close(urlFile);
  43.  
  44.  
  45. # Classify the filters
  46. #
  47. open(filtersFile, $dataDirectory . $filtersFile) || die "Cannot open $filtersFile: $!";
  48. $tagLine= <filtersFile>;
  49. if ($tagLine eq $keepFilterTag)
  50. {
  51.     $omitMatches= $false;                        # filter matches are kept, the rest is dropped
  52. }
  53. else                                                            # default condition
  54. {
  55.     $omitMatches= $true;                        # filter matches are dropped, the rest is kept
  56.     if ($tagLine ne $omitFilterTag)
  57.     {
  58.         seek(filtersFile, 0, 0);            # restart at the beginning of the file as the first line was not a tag
  59.     }
  60. }
  61.  
  62.  
  63. # Read the filters
  64. #
  65. @filters= ();                                            # clear the array
  66. while (<filtersFile>)
  67. {
  68.     if (/$newLineDelimiter$/) { chop; }
  69.     if ($_) { push(@filters, $_); }    # only add non-blank filters
  70. }
  71. close(filtersFile);
  72.  
  73.  
  74. #    Set up input and output files
  75. #
  76. open(linesAddedFile, $dataDirectory . $linesAddedFile) || die "Cannot open $linesAddedFile: $!";
  77. open(newFilesFile, $openToAppend . $workingDirectory . $newFilesFile) || die "Cannot open $newFilesFile: $!";
  78.  
  79.  
  80. #    Filter and add valid lines
  81. #
  82. while(<linesAddedFile>)
  83. {
  84.     if (/$newLineDelimiter$/) { chop; }
  85.     $aLine= $_;
  86.     
  87.     $match= $false;
  88.     foreach $filter (@filters)
  89.     {
  90.         if ($aLine =~ /$filter/)
  91.         {                                                            # we have a match
  92.             $match= $true;
  93.             last;
  94.         }
  95.     }
  96.     if ($omitMatches != $match)
  97.     {                                                                # construct a list item using the path for the URL and the last leaf for the name
  98.         @pathLeaves=    split($unixPathDelimiter, $aLine);
  99.         $aLinkName=     pop (@pathLeaves);
  100.         $aLink= $htmlListItemLeader . $rootURL . $aLine . $htmlListItemMiddle . $aLinkName . $htmlListItemTrailer;
  101.         print (newFilesFile $aLink);
  102.     }
  103. }
  104.  
  105.  
  106. # Clean up
  107. #
  108. close(linesAddedFile);
  109. close(newFilesFile);
  110.